package com.itheima.cases_demo.string_builder;

public class Test {
    public static void main(String[] args) {
        // 拼接数组
        int[] arr = {12, 34, 12, 53};

        String result = appendString(arr);

        System.out.println(result);

        // StringBuilder对象方法测试
        StringBuilder sb = new StringBuilder("start");
        sb.append(" -> 追加一段内容");
        sb.append(true);
        sb.append(100);
        String str = sb.toString();
        System.out.println(str); // start -> 追加一段内容true100

        sb.reverse();
        // 翻转字符串
        System.out.println(sb); // 001eurt容内段一加追 >- trats

        sb.delete(0, 3);
        // 通过字符串下标删除内容，下标是开始和结束
        System.out.println(sb); // eurt容内段一加追 >- trats

        int length = sb.length();
        // 获取字符串长度
        System.out.println(length); // 19
    }

    /**
     * 整形数组拼接
     * @param arr
     * @return
     */
    public static String appendString(int[] arr) {
        StringBuilder sb = new StringBuilder("[");

        for (int i = 0; i < arr.length; i++) {
            sb.append(arr[i]);
            if (i != arr.length - 1) {
                sb.append(",");
            }
        }

        sb.append("]");
        return sb.toString();
    }
}
